package webServiceClient.academico;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

public class FrmCadAluno extends Activity {
	private Button btnCad;
	private Button btnFechar;
	private EditText edtRa;
	private EditText edtNome;
	private EditText edtData;
	private EditText edtEmail;
	private EditText edtSenha;
	private RadioButton rbMasc;

	private void limpaEdt() {
		edtNome.setText("");
		edtData.setText("");
		edtRa.setText("");
		edtEmail.setText("");
		edtSenha.setText("");
		edtRa.requestFocus();
	}

	private void carregaBtn() {
		btnCad = (Button) findViewById(R.FrmCadAluno.btnCadastrar);
		btnFechar = (Button) findViewById(R.FrmCadAluno.btnFechar);
		edtNome = (EditText) findViewById(R.FrmCadAluno.edtNome);
		edtData = (EditText) findViewById(R.FrmCadAluno.edtData);
		edtEmail = (EditText) findViewById(R.FrmCadAluno.edtEmail);
		edtSenha = (EditText) findViewById(R.FrmCadAluno.edtSenha);
		edtRa = (EditText) findViewById(R.FrmCadAluno.edtRa);
		rbMasc = (RadioButton) findViewById(R.FrmCadAluno.rbMasculino);

		btnCad.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				String sexo = "";
				if (rbMasc.isChecked() == true) {
					sexo = "M";
				} else
					sexo = "F";
				try {
					WebServiceClient ws = new WebServiceClient();
					String s = ws.addAluno("addAluno", Definicoes.getRaAdm(),
							Definicoes.getSenhaAdm(),
							Long.parseLong(edtRa.getText().toString()),
							edtNome.getText().toString(),
							edtData.getText().toString(),
							edtEmail.getText().toString(),
							edtSenha.getText().toString(), sexo);
					if (s.equals("true")) {
						Toast t = Toast.makeText(FrmCadAluno.this,
								"Aluno Cadastrado com Sucesso",
								Toast.LENGTH_LONG);
						t.show();
						limpaEdt();
					} else {
						Toast t = Toast.makeText(FrmCadAluno.this,
								"Erro ao cadastrar aluno - "+s, Toast.LENGTH_LONG);
						t.show();
					}
				} catch (Exception e) {
					Toast t = Toast.makeText(FrmCadAluno.this,
							Definicoes._erroServer, Toast.LENGTH_LONG);
					t.show();
				}
			}
		});
		btnFechar.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.frm_cad_aluno);
		carregaBtn();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.frm_cad_aluno, menu);
		return true;
	}

}
